﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Infrastructure.Dapper;
using WoChu.Infrastructure.Data;

using WoChu.WMS.Entity;

namespace WoChu.WMS.Repository
{
    /// <summary>
    /// 订单绑定、审核相关记录
    /// </summary>
    public class WMSToWCSTaskRepository : RepositoryBase<WMSToWCSTask, string>
    {
        public WMSToWCSTaskRepository() : base(ConnectionsDBKey.ERP_Ora) { }
        public List<WMSToWCSTask> GetCheckList(WMSToWCSTask condition)
        {
            StringBuilder sql = new StringBuilder();
            StringBuilder whereSql = new StringBuilder();
            //,u1.UserName CreaterName,create_date,t.fboxno 
            sql.Append(@"
            Select distinct t.OrderNo,t.OrderNo SHEETID,t.other1,
            u2.UserName CheckerName,t.checkdate ,t.check_location,
            u3.UserName EasterEgg_CheckerName,t.EasterEgg_Checkdate,t.EasterEgg_Check_LOCATION,
            s.SHEETID,s.APPORDERNO,
            sai.SHEETID,sai.REMARK,sai.QTY,g.GoodsName
            From SaleAPP s
            left join SaleAPPItem1 sai  on s.SHEETID=sai.SHEETID
            left join goods g on sai.REMARK=g.GoodsCode
            left join wms_to_wcs_task t on s.sheetid=t.orderno            
            left join appuserlist u1 on t.createrid=u1.userid
            left join appuserlist u2 on t.checkerid=u2.userid
            left join appuserlist u3 on t.EasterEgg_Checkerid=u3.userid
            Where 1=1 And (t.checkdate is not null And t.check_location is not null)
            ");
            
            
            if (!string.IsNullOrEmpty(condition.ORDERNO))
                whereSql.Append(" And s.SHEETID=:ORDERNO");
            
            sql.Append(whereSql);

           // var list = Query(sql.ToString(), condition);
            var list = new Dictionary<string, WMSToWCSTask>();
            QueryMulti<WMSToWCSTask, SaleAPP, SaleAPPItem>(sql.ToString(), condition,
                (task, sale, detail) =>
                {
                    WMSToWCSTask s;
                    if (!list.TryGetValue(sale.SHEETID, out s))
                    {
                        s = task;
                        list.Add(sale.SHEETID, task);
                        s.SaleAPP = sale;
                    }
                    s.SaleAPP.SaleAPPItemList.Add(detail);
                    return task;
                },
                "SHEETID");
            return list.Values.ToList();
        }

        
    }
}
